using System;
using System.Collections.Generic;
using System.Text;

namespace Eve.Data
{
    public class Vector
    {
        private double[] v = new double[3];

        public Vector()
        {
        }
        public Vector(double x, double y, double z)
        {
            v[0] = x;
            v[1] = y;
            v[2] = z;
        }
        public Vector(double[] v)
        {
            for (int i = 0; i < 3; i++)
                this.v[i] = v[i];
        }

        public double this[int index]
        {
            get { return v[index]; }
            set { v[index] = value; }
        }
        public double x
        {
            get { return v[0]; }
            set { v[0] = value; }
        }
        public double y
        {
            get { return v[1]; }
            set { v[1] = value; }
        }
        public double z
        {
            get { return v[2]; }
            set { v[2] = value; }
        }
        public double Length
        {
            get { return Math.Sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]); }
        }
        public Vector Unit
        {
            get
            {
                double l = Length;
                if (l == 0.0) return new Vector();
                return new Vector(v[0] / l, v[1] / l, v[2] / l);
            }
        }

        public static implicit operator double[](Vector v)
        {
            return (v == null) ? null : v.v;
        }
        public static Vector operator +(Vector v1, Vector v2)
        {
            return new Vector(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z);
        }
        public static Vector operator -(Vector v1, Vector v2)
        {
            return new Vector(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z);
        }
        public static Vector operator *(Vector v, double s)
        {
            return new Vector(v.x * s, v.y * s, v.z * s);
        }
        public static Vector operator *(double s, Vector v)
        {
            return new Vector(v.x * s, v.y * s, v.z * s);
        }

        public double Dot(Vector o)
        {
            return v[0] * o[0] + v[1] * o[1] + v[2] * o[2];
        }
        public Vector Cross(Vector u)
        {
            return new Vector(v[1] * u[2] - v[2] * u[1], v[2] * u[0] - v[0] * u[2], v[0] * u[1] - v[1] * u[0]);
        }
    }
}
